$(function () {
    InitTable();
    //根据窗口调整表格高度
    $(window).resize(function() {

        $('#peopleLogTab').bootstrapTable('resetView', {
            height: tableHeight()
        })
    });
    $("#searchPeopleLogBtn").click(function () {
        InitTable();
    });
    function InitTable() {
        $('#peopleLogTab').bootstrapTable('destroy');
        $('#peopleLogTab').bootstrapTable({
            url: "/peoplelog/selectAllPeopleLog",//数据源
            height: tableHeight(),//高度调整
            //search: true,//是否搜索
            pagination: true,//是否分页
            pageSize: 8,//单页记录数
            pageList: [5, 10, 20, 50],//分页步进值
            sidePagination: "client",//服务端分页
            contentType: "application/x-www-form-urlencoded",//请求数据内容格式 默认是 application/json 自己根据格式自行服务端处理
            dataType: "json",//期待返回数据类型
            method: "post",//请求方式
            //searchAlign: "right",//查询框对齐方式
            queryParamsType: "limit",//查询参数组织方式
            uniqueId: "pl_id",
            queryParams: function getParams(params) {
                return {
                    s_name:$("#pl_nameSearch").val(),
                    s_card:$("#pl_cardSearch").val(),
                    pl_license:$("#pl_licenseSearch").val(),
                    state_name:$("#pl_stateSearch").val()
                }
            },
            searchOnEnterKey: false,//回车搜索
            //showRefresh: true,//刷新按钮
            //showColumns: true,//列选择按钮
            buttonsAlign: "right",//按钮对齐方式
            toolbar: "toolbar", //指定工具栏
            toolbarAlign: "right",//工具栏对齐方式
            columns: [
                {
                    title: "全选",
                    field: "select",
                    checkbox: true,
                    width: 20,//宽度
                    align: "center",//水平
                    valign: "middle"//垂直
                },
                {
                    field: "pl_id",//键名
                    title: "id",//标题
                    sortable: true,//是否可排序
                    order: "desc"//默认排序方式
                },
                {
                    field: "pl_card",
                    title: "员工编号",
                    algin:"center",
                    sortable: true
                },
                {
                    field: "pl_name",
                    title: "员工姓名",
                    sortable: true
                },
                {
                    field: "pl_workdays",
                    title: "工作天数",
                    width:25,
                    align:"center",
                    sortable: true
                },

                {
                    field: "pl_diverdays",
                    title: "出车天数",
                    sortable: true
                },
                {
                    field: "pl_license",
                    title: "驾照类型",
                    sortable: true
                },
                {
                    field: "state_name",
                    title: "员工状态",
                    width:30,
                    align:"center",
                    sortable: true
                },

            ],
            onClickRow: function (row, $element) {
                //$element是当前tr的jquery对象
                $element.css("background-color", "#cecece");
            },//单击row事件
            locale: "zh-CN", //中文支持
            detailView: false, //是否显示详情折叠
            /*detailFormatter: function(index, row, element) {
                var html = '';
                $.each(row, function(key, val){
                    html += "<p>" + key + ":" + val +  "</p>"
                });
                return html;
            }*/
        });
    }
    function tableHeight() {
        return $(window).height() - 50;
    }

    $("#updatePeopleBtn").click(function () {
        $("#addModal").modal("show");
        $("#myDialogHeaderLable").text("更新记录");
        $("#addRecord").text("更新");
        var table = $("#peopleLogTab").bootstrapTable("getSelections");
        if(table !=undefined && table.length === 1){
            var result = table[0];
            $("#pl_id").val(result.pl_id),
            $("#pl_workdays").val(result.pl_workdays);
            $("#pl_diverdays").val(result.pl_diverdays),
            $("#pl_license").val(result.pl_license),
            $("#pl_state").val(result.pl_state),
            $("#pl_name").val(result.pl_name),
            $("#pl_card").val(result.pl_card)
        }
    });
    $("#addRecord").click(function () {
        var pl_id = $("#pl_id").val();
            $.ajax({
                url:"/peoplelog/updatePeopleLog",
                type:"post",
                dataType:"json",
                data: {
                    pl_id:pl_id,
                    pl_workdays: $("#pl_workdays").val(),
                    pl_diverdays: $("#pl_diverdays").val(),
                    pl_license: $("#pl_license").val(),
                    pl_state: $("#pl_state").val(),
                },
                success:function (result) {
                    if (result.errorCode == 0){
                        toastr.success(result.status);
                        $("#addModal").modal("hide");
                        $("#peopleLogTab").bootstrapTable("refresh");
                        clearModelData();
                    }else {
                        toastr.error("更新失败")
                    }
                }
            })

    });
    function clearModelData() {
        $("#pl_id").val("");
        $("#pl_workdays").val(""),
        $("#pl_driverdays").val(""),
        $("#pl_license").val(""),
        $("#pl_state").val("")
    }
});
